package com.yongninger.activity.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.yongninger.activity.MainActivity;
import com.yongninger.activity.R;
import com.yongninger.adapter.UserListViewAdapter;
import com.yongninger.dao.UserDao;
import com.yongninger.vo.UserVo;

public class ListActivity  extends android.app.ListActivity implements OnScrollListener {
	private UserDao userDao;
	/*分页*/
	private ListView listView;
	private int visibleLastIndex = 0;	//最后的可视项索引
	private int visibleItemCount;		// 当前窗口可见项总数
	private UserListViewAdapter adapter;
	private View loadMoreView;
	private Button loadMoreButton;
	private Handler handler = new Handler();
	private int pagesize = 10;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_list);
		userDao = new UserDao(getApplicationContext());
		loadMoreView = getLayoutInflater().inflate(R.layout.load_more, null);
		loadMoreButton = (Button) loadMoreView.findViewById(R.id.loadMoreButton);
		listView = getListView();				//获取id是list的ListView
		listView.addFooterView(loadMoreView);	//设置列表底部视图
		initAdapter();
		setListAdapter(adapter);				//自动为id是list的ListView设置适配器
		listView.setOnScrollListener(this);		//添加滑动监听
		// 条目点击事件
		listView.setOnItemClickListener(new ItemClickListener());
		
		// 新增按钮点击事件
		Button addBtn = (Button) this.findViewById(R.id.add);
		OnClickListener onClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SharedPreferences userInfo = getSharedPreferences("user_info", 0);
				String umenu52 = userInfo.getString("umenu52", "");
				if("1".equals(umenu52)){
					Intent intent = new Intent(ListActivity.this, AddActivity.class);
					startActivity(intent);
				}else{
					Toast.makeText(getApplicationContext(), "您无权限编辑",
							Toast.LENGTH_SHORT).show();
				}
			}
		};
		addBtn.setOnClickListener(onClickListener);
	}

	// 获取点击事件
	private final class ItemClickListener implements OnItemClickListener {

		@SuppressWarnings("unchecked")
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			ListView listView = (ListView) parent;
			HashMap<String, Object> data = (HashMap<String, Object>) listView
					.getItemAtPosition(position);
			Intent intent = new Intent(ListActivity.this, ViewActivity.class);
			UserVo userVo = new UserVo();
			userVo.setUid(Integer.parseInt((String.valueOf(data.get("uid")))));
			userVo.setUname(String.valueOf(data.get("uname")));
			userVo.setUpwd(String.valueOf(data.get("upwd")));
			userVo.setUtype(String.valueOf(data.get("utype")));
			userVo.setUmenu1(String.valueOf(data.get("umenu1")));
			userVo.setUmenu11(String.valueOf(data.get("umenu11")));
			userVo.setUmenu12(String.valueOf(data.get("umenu12")));
			userVo.setUmenu2(String.valueOf(data.get("umenu2")));
			userVo.setUmenu21(String.valueOf(data.get("umenu21")));
			userVo.setUmenu22(String.valueOf(data.get("umenu22")));
			userVo.setUmenu3(String.valueOf(data.get("umenu3")));
			userVo.setUmenu31(String.valueOf(data.get("umenu31")));
			userVo.setUmenu32(String.valueOf(data.get("umenu32")));
			userVo.setUmenu4(String.valueOf(data.get("umenu4")));
			userVo.setUmenu41(String.valueOf(data.get("umenu41")));
			userVo.setUmenu42(String.valueOf(data.get("umenu42")));
			userVo.setUmenu5(String.valueOf(data.get("umenu5")));
			userVo.setUmenu51(String.valueOf(data.get("umenu51")));
			userVo.setUmenu52(String.valueOf(data.get("umenu52")));
			userVo.setUmenu6(String.valueOf(data.get("umenu6")));
			userVo.setUmenu61(String.valueOf(data.get("umenu61")));
			userVo.setUmenu62(String.valueOf(data.get("umenu62")));
			intent.putExtra("userVo", userVo);
			startActivity(intent);
		}
	}
	/**
	 * 初始化适配器
	 */
	private void initAdapter() {
		List<UserVo> userVos = userDao.queryUserVoList(null,0,pagesize);
		List<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();
		for (UserVo vo : userVos) {
			HashMap<String, Object> item = new HashMap<String, Object>();
			item.put("uid", vo.getUid());
			item.put("uname", vo.getUname());
			item.put("upwd", vo.getUpwd());
			if("0".equals(vo.getUtype())){
				item.put("utype", "管理员");
			}else{
				item.put("utype", "普通用户");
			}
			item.put("umenu1", vo.getUmenu1());
			item.put("umenu11", vo.getUmenu11());
			item.put("umenu12", vo.getUmenu12());
			item.put("umenu2", vo.getUmenu2());
			item.put("umenu21", vo.getUmenu21());
			item.put("umenu22", vo.getUmenu22());
			item.put("umenu3", vo.getUmenu3());
			item.put("umenu31", vo.getUmenu31());
			item.put("umenu32", vo.getUmenu32());
			item.put("umenu4", vo.getUmenu4());
			item.put("umenu41", vo.getUmenu41());
			item.put("umenu42", vo.getUmenu42());
			item.put("umenu5", vo.getUmenu5());
			item.put("umenu51", vo.getUmenu51());
			item.put("umenu52", vo.getUmenu52());
			item.put("umenu6", vo.getUmenu6());
			item.put("umenu61", vo.getUmenu61());
			item.put("umenu62", vo.getUmenu62());
			items.add(item);
		}
		adapter = new UserListViewAdapter(this, items);
		
	}

	/**
	 * 滑动时被调用
	 */
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		this.visibleItemCount = visibleItemCount;
		visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
	}

	/**
	 * 滑动状态改变时被调用
	 */
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		int itemsLastIndex = adapter.getCount() - 1;	//数据集最后一项的索引
		int lastIndex = itemsLastIndex + 1;				//加上底部的loadMoreView项
		if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) {
			//如果是自动加载,可以在这里放置异步加载数据的代码
			Log.i("LOADMORE", "loading...");
		}
	}
	
	/**
	 * 点击按钮事件
	 * @param view
	 */
	public void loadMore(View view) {
		loadMoreButton.setText("loading...");	//设置按钮文字loading
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				
				loadData();
				
				adapter.notifyDataSetChanged();	//数据集变化后,通知adapter
				listView.setSelection(visibleLastIndex - visibleItemCount + 1);	//设置选中项
				
				loadMoreButton.setText("load more");	//恢复按钮文字
			}
		}, 2000);
	}
	
	/**
	 * 模拟加载数据
	 */
	private void loadData() {
		int count = adapter.getCount();
		List<UserVo> userVos = userDao.queryUserVoList(null,count,pagesize);
		for (UserVo vo : userVos) {
			HashMap<String, Object> item = new HashMap<String, Object>();
			item.put("uid", vo.getUid());
			item.put("uname", vo.getUname());
			item.put("upwd", vo.getUpwd());
			if("0".equals(vo.getUtype())){
				item.put("utype", "管理员");
			}else{
				item.put("utype", "普通用户");
			}
			item.put("umenu1", vo.getUmenu1());
			item.put("umenu11", vo.getUmenu11());
			item.put("umenu12", vo.getUmenu12());
			item.put("umenu2", vo.getUmenu2());
			item.put("umenu21", vo.getUmenu21());
			item.put("umenu22", vo.getUmenu22());
			item.put("umenu3", vo.getUmenu3());
			item.put("umenu31", vo.getUmenu31());
			item.put("umenu32", vo.getUmenu32());
			item.put("umenu4", vo.getUmenu4());
			item.put("umenu41", vo.getUmenu41());
			item.put("umenu42", vo.getUmenu42());
			item.put("umenu5", vo.getUmenu5());
			item.put("umenu51", vo.getUmenu51());
			item.put("umenu52", vo.getUmenu52());
			item.put("umenu6", vo.getUmenu6());
			item.put("umenu61", vo.getUmenu61());
			item.put("umenu62", vo.getUmenu62());
			adapter.addItem(item);
		}
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			// 跳转到查询页面
			Intent intent = new Intent(ListActivity.this,
					MainActivity.class);
			startActivity(intent);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
